<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 
      在子组件调用父组件的方法
        1. 在父组件的子组件标签上面添加自定义事件
        2. 在子组件里面调用this.$emit("xxx")
     -->
    <!-- 
      子传父
        在this.$emit("xxx", yyy)里面传递第二个参数,yyy表示我要传递的数据
        在父组件就可以通过参数接受到了
      -->
    <div id="app">
      <h2>组件传参-子传父 - {{ fatherMsg }}</h2>
      <child @abc="fn"></child>
    </div>

    <script src="./lib/Vue3.js"></script>
    <script>
      const { createApp } = Vue;

      const Child = {
        data() {
          return {
            msg: "我是子组件",
          };
        },
        template: `
          <div>
            <div>child组件</div>
            <button @click="handleClick">btn</button>
          </div>
        `,
        methods: {
          handleClick() {
            this.$emit("abc", this.msg);
          },
        },
        mounted() {
          this.$emit("abc", this.msg);
        },
      };

      createApp({
        data() {
          return {
            fatherMsg: "",
          };
        },
        methods: {
          fn(msg) {
            console.log("fn", msg);
            this.fatherMsg = msg;
          },
        },
        components: {
          Child,
        },
      }).mount("#app");
    </script>
  </body>
</html>
